﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Grid;

namespace QuanLyKho.PhongLenh
{
    public partial class Lss_DanhSachLenhPopup : Form
    {
        public Lss_DanhSachLenhPopup()
        {
            InitializeComponent();
            Loaddata();
            SetPropertiesGrid();
            SetPropertiesColumns();
        }

        public void Loaddata()
        {
            DataTable data = DataAll.SelectAll("LenhSanXuat inner join PhongMau_SanPham On LenhSanXuat.IdSanPham = PhongMau_SanPham.IdSanPham",
                "LenhSanXuat.[ID],PhongMau_SanPham.[MaHang] as MaSanPham,LenhSanXuat.[IdSanPham],[NgayLapLenh],[NguoiLapLenh],LenhSanXuat.[SoLuong],[NgayGuiLenh],[NgaySuaLenh],[NgayDuyetLenh],LenhSanXuat.[TrangThai],[NgayChungTu],[NgayHoachToan],LenhSanXuat.[SoChungTu],LenhSanXuat.[GhiChu],[KhoDen],[KhoDi],[SXTuNgay],[SXDenNgay]",
                "WHERE LenhSanXuat.TrangThai = " + Convert.ToInt32(TrangThai.LenhSanXuat_ChoDuyet));
            gridLenhChoDuyet.DataSource = data;
        }

        public void SetPropertiesGrid()
        {
            gvLenhChoDuyet.BestFitColumns();
            gvLenhChoDuyet.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            gvLenhChoDuyet.OptionsSelection.MultiSelect = true;
            gvLenhChoDuyet.OptionsView.ShowGroupPanel = false;
            gvLenhChoDuyet.OptionsView.ShowFooter = true;
            gvLenhChoDuyet.OptionsFind.AlwaysVisible = true;
            gvLenhChoDuyet.Columns["ID"].Visible = false;
            for (int i = 3; i < gvLenhChoDuyet.Columns.Count; i++)
            {
                gvLenhChoDuyet.Columns[i].OptionsColumn.AllowEdit = false;
            }
        }

        public void SetPropertiesColumns()
        {
            gcDuyetLenh.Width = 60;
            gcDuyetLenh.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcKhongDuyet.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcMaSanPham1.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayLapLenh1.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNguoiLapLenh1.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcSoLuong1.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayGuiLenh1.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcTrangThai1.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayChungTu1.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcNgayHoachToan1.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcSoChungTu1.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcGhiChu1.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
        }

        private void gvLenhChoDuyet_DoubleClick(object sender, EventArgs e)
        {
            string a = gvLenhChoDuyet.GetRowCellValue(gvLenhChoDuyet.FocusedRowHandle, "TrangThai").ToString();
            string b = Convert.ToInt32(TrangThai.LenhSanXuat_ChoDuyet).ToString();
            if (a == b)
            {
                if (gvLenhChoDuyet.FocusedRowHandle >= 0)
                {
                    Lss_LapLenh.MaID = gvLenhChoDuyet.Columns["ID"].View.GetRowCellDisplayText(gvLenhChoDuyet.FocusedRowHandle, "ID");
                    var lss = new Lss_LapLenh();
                    lss.ShowDialog();
                    Loaddata();
                    Lss_LapLenh.MaID = "0";
                }
            }
        }

        private void gvLenh_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "TrangThai")
            {
                e.DisplayText = Common.HienThiTrangThai(Convert.ToInt32(e.Value));
            }
        }

        private void gvLenhChoDuyet_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "SXDenNgay")
            {
                TimeSpan songay = Convert.ToDateTime(e.CellValue) - DateTime.Now;
                if (0 < songay.Days && songay.Days < 5)
                {
                    e.Appearance.BackColor = Color.PaleGoldenrod;
                }
                if (songay.Days < 0)
                {
                    e.Appearance.BackColor = Color.DarkGray;
                }
            }
        }
    }
}
